﻿using System;
using System.IO;
using System.Web.Mvc;
using System.Web.Security;
using NOSDC.Areas.Admin.Models;
using NOSDC.Pocos;
using NOSDC.Repositories.Documents;

namespace NOSDC.Areas.Admin.Controllers
{
    public class DocumentsController : BaseController
    {
        private readonly IDocumentRepository _repository;
        public DocumentsController()
        {
            _repository = new DocumentRepository();
        }

        // GET: /Admin/Documents/
        public ActionResult Index()
        {
            return View(_repository.GetDocuments());
        }

        public ActionResult CreateDocument()
        {
            return View(new DocumentModelView());
        }

        [HttpPost]
        public ActionResult UploadFile(string qqFile)
        {
            try
            {
                var path = Server.MapPath("~/UploadedFiles");
                var file = Path.Combine(path, qqFile);
                var fileSize = 0L;
                var fileType = qqFile.Substring(qqFile.LastIndexOf(".") + 1);

                using (var output = System.IO.File.Create(file))
                {
                    Request.InputStream.CopyTo(output);
                    fileSize = Request.InputStream.Length;
                }

                return Json(new { success = true, FileSize = fileSize, FileType = fileType, FileName = qqFile, UploadDate = DateTime.Now.ToString() });
            }
            catch (Exception e)
            {
                return Json(new { success = false, Message = e.Message });
            }
        }

        [HttpPost]
        public ActionResult Delete(int? id)
        {
            var doc = _repository.GetDocumentById((int)id);

            if (doc != null)
            {
                _repository.DeleteDocument(doc);
            }

            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult CreateDocument(DocumentModelView model)
        {
            if (ModelState.IsValid)
            {
                model.Document.Filepath = "/UploadedFiles/" + model.Document.FileName;
                model.Document.DocumentType = model.Document.FileName.Substring(model.Document.FileName.LastIndexOf(".") + 1);
                var user = Membership.GetUser();
                if (user != null)
                {
                    model.Document.UploadedBy = new Guid(user.ProviderUserKey.ToString());
                }

                _repository.CreateDocument(model.Document);
                return RedirectToAction("Index");
            }

            return View(model);
        }

        public ActionResult UpdateDocument(int? id)
        {
            if (id != null)
                return View(_repository.GetDocumentById((int)id));

            return View();
        }

        [HttpPost]
        public ActionResult UpdateDocument(Document doc)
        {
            doc.UploadDate = DateTime.Now;
            doc.UploadedBy = new Guid(Membership.GetUser().ProviderUserKey.ToString());
            _repository.UpdateDocument(doc);

            return RedirectToAction("Index");
        }
    }
}
